<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Packages;
use app\common\model\UserPackagesConfig;
use fast\Random;

/**
 * 开卡
 */
class Activatecard extends Api
{
    protected $noNeedLogin = ['address','haoma','orderSync','lianyun_address'];
    protected $noNeedRight = ['*'];

    public function address(){
        $data['appid']     = config('site.ooen_appid');
        $data['timestamp'] = time();
        $data['nonce_str'] = Random::alnum(32);
        $params            = input('post.');

        $info = Packages::where(['id'=>$params['id'],'status'=>1])->find();
        if(!$info){
            $this->error('产品不存在或已下架');
        }
        if($info['platform_type'] == 8){
            $this->success('ok',$this->lianyun_address());
        }
        $params['id']      = $info['packages_id'];
        $data              = array_merge($data,$params);
        $string            = ascll($data);
        $stringSignTemp  = $string.'&appsecret='.config('site.open_appsecret');;
        $data['sign']    = mb_strtoupper( md5($stringSignTemp));
        $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/area";//获取地区
//        $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/packageOrder";//办理套餐
        $res = httpPost($url,$data);
        $arr = json_decode($res,true);
        if(empty($arr['area'])){
            $this->error('套餐不存在');
        }
        $this->success('ok',$arr['area']);
    }

    /**
     * @menu 联云地址
     * @Author:男人的坏
     * @Date:2024-05-14 14:42
     */
    public function lianyun_address(){
        $data = db('yunlian_address')->where(['pid'=>0])->select();
        foreach ($data as $k=>&$v){
            $v['child'] = db('yunlian_address')->where(['pid'=>$v['id']])->select();
            if($v['child']){
                foreach ($v['child'] as $kk=>&$vv){
                    $vv['child'] = db('yunlian_address')->where(['pid'=>$vv['id']])->select();
                }
            }
        }
        return $data;
    }

    public function haoma(){
        $cardType = input('post.cardType');
        if(!$cardType){
            $this->error('缺少参数');
        }
        $provinceName = input('post.provinceName');
        if(!$provinceName){
            $this->error('缺少参数');
        }
        $cityName = input('post.cityName');
        if(!$cityName){
            $this->error('缺少参数');
        }
        $info = Packages::where(['id'=>(int)$cardType,'status'=>1])->find();
        if(!$info){
            $this->error('产品不存在或已下架');
        }
        $searchValue = input('post.searchValue');
          if($info['platform_type'] == 2){
              $params['partnerId']    = config('site.hao2_partnerId');
              $secret                 = config('site.hao2_secret');
              $url                    = config('site.hao2_url')."/selectNumber";
          }else{
              $params['partnerId']    = config('site.hao3_partnerId');
              $secret                 = config('site.hao3_secret');
              $url                    = config('site.site.hao3_url')."/selectNumber";
          }
        $params['provinceName'] = str_replace('省', '', $provinceName);
        $params['cityName']     = str_replace('省', '', $cityName);
          //，343、345是海南三亚市。165是浙江宁波市
          if($info['id']== 132 || $info['id'] == 146 || $info['id'] == 155 || $info['id'] == 181 || $info['id'] == 182){
              $params['provinceName'] = config('site.lc_provinceName');
              $params['cityName']     = config('site.lc_cityName');
          }elseif($info['id']== 343 || $info['id']== 345){
              $params['provinceName'] = "海南";
              $params['cityName']     = "三亚市";
          }elseif ($info['id']== 165){
              $params['provinceName'] = "浙江";
              $params['cityName']     = "宁波市";
          }else{
              $params['provinceName'] = $provinceName;
              $params['cityName']     = $cityName;
          }
        if ($params['cityName'] == "市辖区" || $params['cityName'] == "县") {
            $params['cityName']     = $provinceName;
            $params['provinceName'] = str_replace('市', '', $provinceName);

        }
        if($info['id']== 371 ){
            $params['provinceName'] = "湖南";//省
            $params['cityName']     = "长沙市";//市
//            $params['postDistrictName'] = '';
        }

        if($info['id']== 440 ||  $info['id']== 441 ){
            $params['provinceName'] = "广东";//省
            $params['cityName']     = "广州市";//市
//            $params['postDistrictName'] = '';
        }
        if ($info['province_name']) {
            $params['provinceName'] = $info['province_name'];
        }
        if ($info['city_name']) {
            $params['cityName'] = $info['city_name'];//市
        }
          $params['cardType']     = $info['packages_id'];
          if($searchValue){
              $params['searchValue'] = $searchValue;
          }
        if ($info['province_name']) {
            $params['provinceName'] = $info['province_name'];
        }
        if ($info['city_name']) {
            $params['cityName'] = $info['city_name'];//市
        }
          $params['pageNo']          = input('post.pageNo',1);
//        $params['certName'] = '朱胜楠';
//        $params['certNo'] = '410211199104060014';
//        $params['contactNum'] = '15890956946';
          ksort($params);//升序排序
          $str = '';
          foreach ($params as $k => $v) {
              $str .=  $k  . $v;  //cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150
          }
          $str = $secret.$str.$secret;//abc123cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150222abc123
          $sign=strtoupper(md5($str)); //93932E8BD15A2EBE6D0C4F3890B97D90
          $params['sign'] = $sign;
          $res = json_decode(httpPost($url, $params),true);
        if(empty($res['data']['numArray'])){
            unset($params['sign']);
            $params['certName'] = input('certName');
            $params['certNo'] =  input('certNo');
            $params['contactNum'] = input('contactNum');
            ksort($params);//升序排序
            $str = '';
            foreach ($params as $k => $v) {
                $str .=  $k  . $v;  //cardType4cityName郑州市pageNo1partnerId1proniceName河南searchValue4150
            }
            $str = $secret.$str.$secret;
            $sign=strtoupper(md5($str));
            $params['sign'] = $sign;
            $res = json_decode(httpPost($url, $params),true);
        }
          if(!empty($res['data']['numArray'])){
              $res['data']['numArrayTwo'] = [];
              foreach ($res['data']['numArray'] as $k=>$v){
                  $res['data']['numArrayTwo'][$k]['id']   = $k+1;
                  $res['data']['numArrayTwo'][$k]['name'] = $v;
                  if($res['data']['checkList']){
                      $res['data']['numArrayTwo'][$k]['code'] = $res['data']['checkList'][$k];
                  }
//                  $res['data']['numArrayTwo'][$k]['code'] = $res['data']['checkList'][$k];
              }
//              dump($res);
              $this->success('ok',$res);
          }else{
              $this->error('无法选号');
          }

//         $this->error($res['info']);
    }

  public function isMunicipality($text) {
        $municipalities    = array("北京市", "天津市", "上海市", "重庆市");
        if (in_array($text, $municipalities)) {
            return true;
        } else {
            return false;
        }
    }
}
